package fr.ece.ing4.si.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.swing.*;

import fr.ece.ing4.si.multithread.MonteCarloSimulation2;
import fr.ece.ing4.si.singlethread.MonteCarloSimulation;

public class Window extends JFrame{

	private JLabel label1;
	private DefaultComboBoxModel comboModel;
	private JComboBox combo; 
	private String ligne;
	private JPanel panel1;
	private JButton single;
	private JButton multi;
	private String comboSet;
	public Window() throws IOException
	
	
	
	
	{
		super();
		this.getContentPane();
		this.setLayout(null);
		ligne=null;
		label1=new JLabel();
		comboModel=new DefaultComboBoxModel();
		combo=new JComboBox(comboModel);
		single=new JButton("SingleThread");
		multi=new JButton("MultiThread");
		single.setBounds(20,20,150,50);
		multi.setBounds(175,20,150,50);
		
		
		
		BufferedReader reader =  new BufferedReader(new FileReader("simulation.txt"));
		while((ligne=reader.readLine())!=null)
		{
			comboModel.addElement(" "+ligne);
		}
		
		combo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				
				comboSet=(String.valueOf(((JComboBox)e.getSource()).getSelectedItem()));
				System.out.println("Combo" +comboSet);
				
				
			}

			
		});
		
		multi.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				final String [] data=comboSet.split(",");
				MonteCarloSimulation2 m=new MonteCarloSimulation2();
				
				
				try {
					try {
						setLabel(String.valueOf(m.MultiMonteCarlo2(data[0],Double.valueOf(data[1]),Double.valueOf(data[2]),Double.valueOf(data[3]),Double.valueOf(data[4]),Double.valueOf(data[5]),Double.valueOf(data[6]),Integer.valueOf(data[7]),Integer.valueOf(data[8]))));
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ExecutionException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			});
		
		
		single.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				final String [] data=comboSet.split(",");
				MonteCarloSimulation m=new MonteCarloSimulation();
				
				
				try {
					setLabel(String.valueOf(m.SingleMonteCarlo2(data[0],Double.valueOf(data[1]),Double.valueOf(data[2]),Double.valueOf(data[3]),Double.valueOf(data[4]),Double.valueOf(data[5]),Double.valueOf(data[6]),Integer.valueOf(data[7]),Integer.valueOf(data[8]))));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			});
		
		
			
		panel1=new JPanel();
		panel1.add(combo);
		panel1.setBounds(20,100,300,50);
		label1.setBounds(20,170,300,50);
		setTitle("MonteCarloSimulation");
		setSize(500,600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(single);
		this.add(multi);
		this.add(panel1);
		this.add(label1);
				
	}
	
	
	public String getComboSet()
	{
		return comboSet;
	}
	
	public void setLabel(String p)
	{
		label1.setText(p);
		
	
	}
	

}
